package com.li.onjava8.concurrent.cf.deadlock;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class StickHolder {
    private static class Chopstick {}
    private Chopstick stick = new Chopstick();
    private BlockingQueue<Chopstick> holder = new ArrayBlockingQueue<>(1);

    public StickHolder() {
        putDown();
    }

    //拿筷子
    public void pickUp() {
        try {
            holder.take();// Blocks if unavailable
        } catch(InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    //放筷子
    public void putDown() {
        try {
            holder.put(stick);
        } catch(InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

}
